package com.stars.home.activity

import android.Manifest
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.KeyEvent
import androidx.lifecycle.lifecycleScope
import com.eazypermissions.coroutinespermission.PermissionManager
import com.githang.statusbar.StatusBarCompat
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.base.MBActivity
import com.stars.home.config.ConfigRx
import com.stars.home.databinding.ActivityOneloginBinding
import com.stars.home.inte.ChoiceData
import com.stars.home.jupsh.componnent.JpushUtils.initPush
import com.stars.home.utils.PopuLimitUtils
import com.stars.home.vmodel.OneLoginVModel
import com.tencent.mmkv.MMKV
import com.trailer.base.utils.KeySimulateUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 * 一键登录
 *
 *on 2020/9/9
 */
class OneLoginActivity : MBActivity<OneLoginVModel, ActivityOneloginBinding>() {

    var isShow = false

    override fun onResume() {
        super.onResume()

        if (isShow) {
            isShow = false

            var isEnable = MMKV.defaultMMKV().getBoolean("limits", false)
            if (!isEnable) {
                PopuLimitUtils.show(context(), binding.cl, object : ChoiceData {
                    override fun onRestuse(id: String?) {
                        if (id.equals("0")) {//不开启友盟统计
                            //关闭应用
                            isShow = true
                            val home = Intent(Intent.ACTION_MAIN)
                            home.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                            home.addCategory(Intent.CATEGORY_HOME)
                            startActivity(home)

                        } else {
//                            UpdateUtils.init(context = applicationContext)
                            initPush(context = applicationContext)

                            lifecycleScope.launch {
                                PermissionManager.requestPermissions(
                                    this@OneLoginActivity, 1000,
                                    Manifest.permission.ACCESS_COARSE_LOCATION,
                                    Manifest.permission.ACCESS_FINE_LOCATION,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.READ_EXTERNAL_STORAGE,

                                )
                            }

                        }

                    }

                })


            }
        }
    }


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#00ffffff"))

        launch {
            flow<String> {
                delay(1000)
                emit("") // emit next value
            }.collect {
                var isEnable = MMKV.defaultMMKV().getBoolean("limits", false)
                if (!isEnable) {

                    PopuLimitUtils.show(context(), binding.cl, object : ChoiceData {
                        override fun onRestuse(id: String?) {
                            if (id.equals("0")) {//不开启友盟统计
                                //关闭应用
                                isShow = true
                                val home = Intent(Intent.ACTION_MAIN)
                                home.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                                home.addCategory(Intent.CATEGORY_HOME)
                                startActivity(home)

                            } else {

//                                UpdateUtils.init(context = applicationContext)
                                initPush(context = applicationContext)

                                lifecycleScope.launch {
                                    PermissionManager.requestPermissions(
                                        this@OneLoginActivity, 1000,
                                        Manifest.permission.ACCESS_COARSE_LOCATION,
                                        Manifest.permission.ACCESS_FINE_LOCATION,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                        Manifest.permission.READ_EXTERNAL_STORAGE,
                                    )
                                }
                            }

                        }

                    })

                }
            }
        }

//        //测试升级
//
//        val data = UpdateBean(2,"1.0.1","发现新版本","1、修复了已知可能影响体验的BUG\n" +
//                "                \\n2、提升直播及在线聊天的稳定性\\n3、多种优化等你发现","https://seekyeah.oss-cn-hongkong.aliyuncs.com/image/2022-09-151663245691407IG 濾鏡構思.docx",0,0)  //这是我这里后台返回的Model，你可以根据自己项目进行修改
//        showUpgradeDialog(data)


    }

    override fun listeners() {
        super.listeners()
        binding.imgSure.onClick {
            if(vm.isRead.value==0){
                binding.imgSure.setImageResource(R.drawable.me_read_on)
                vm.isRead.value = 1
            }else{
                binding.imgSure.setImageResource(R.drawable.me_read_off)
                vm.isRead.value = 0
            }
        }
        binding.llSure.onClick {
            if(vm.isRead.value==0){
                binding.imgSure.setImageResource(R.drawable.me_read_on)
                vm.isRead.value = 1
            }else{
                binding.imgSure.setImageResource(R.drawable.me_read_off)
                vm.isRead.value = 0
            }
        }
    }

    override fun binding(): ActivityOneloginBinding {
        return ActivityOneloginBinding.inflate(layoutInflater)
    }


    override fun title(): String? {
        return null
    }

    override fun vmClass(): Class<OneLoginVModel> {
        return OneLoginVModel::class.java
    }


    override val isRxbus: Boolean
        get() = true


    @Subscribe(tags = [Tag(ConfigRx.ADD_ONELOGIN)])
    fun setOneLogin(token: String) {
        vm.oneLogin(token)
    }



    @Subscribe(tags = [Tag(ConfigRx.ADD_SEND_CODE)])
    fun setLoginCode(code: String) {
        vm.login(code)
    }
    @Subscribe(tags = [Tag(ConfigRx.CLOSE_LOGIN)])
    fun setClose(code: String) {
       finish()
    }

    override fun transluent(): Boolean {
        return true
    }


    override fun onBackPressed() {
        KeySimulateUtils.simulate(KeyEvent.KEYCODE_HOME)
    }



}

